package com.witframework.web.tag.tab;

import java.util.Vector;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * <t:tabpanel id="panel1" width="400px" height="300px"> <t:panel selected="true"> <t:panelTab left="0" title="tab
 * number 1"> Tab 1</t:panelTab> <t:panelBody> your JSP code for panel 1</t:panelBody> </t:panel> <t:panel>
 * <t:panelTab left="91"> Tab 2</t:panelTab> <t:panelBody> your JSP code for panel 2</t:panelBody> </t:panel>
 * <t:panel> <t:panelTab left="182"> Tab 3</t:panelTab> <t:panelBody> your JSP code for panel 3</t:panelBody>
 * </t:panel> </t:tabpanel>
 * 
 * @author wuxinyang Aug 27, 2009 9:50:10 AM
 */
public class tabpanelTag extends BodyTagSupport implements tabbedpanel {
	private String id = null;
	private String width = null;
	private String height = null;
	private String lastWish = null;
	private String activeColor = "#d3d3d3";
	private String inactiveColor = "#e8e8e8";
	private Vector panels = null;

	public void setId(String paramString) {
		this.id = paramString;
	}

	public String getId() {
		return this.id;
	}

	public void setWidth(String paramString) {
		this.width = paramString;
	}

	public String getWidth() {
		return this.width;
	}

	public void setHeight(String paramString) {
		this.height = paramString;
	}

	public String getHeight() {
		return this.height;
	}

	public void setActiveColor(String paramString) {
		this.activeColor = paramString;
	}

	public String getActiveColor() {
		return this.activeColor;
	}

	public void setInactiveColor(String paramString) {
		this.inactiveColor = paramString;
	}

	public String getInactiveColor() {
		return this.inactiveColor;
	}

	public void addPanel(Panel paramPanel) {
		if (this.panels == null)
			this.panels = new Vector();		
		this.panels.addElement(paramPanel);
	}

	public void setLastWish(String paramString) {
		if (this.lastWish == null)
			this.lastWish = paramString;
		else
			this.lastWish += paramString;
	}

	public String getLastWish(String paramString) {
		return this.lastWish;
	}

	public int doAfterBody() throws JspException {
		Panel localPanel;
		int i;
		BodyContent localBodyContent = getBodyContent();
		JspWriter localJspWriter = localBodyContent.getEnclosingWriter();
		StringBuffer localStringBuffer = new StringBuffer("");
		int l = 0;
		if (this.pageContext.getAttribute("tbbdpnlmrkcj2005" + this.id, 1) == null) {
			this.pageContext.setAttribute("tbbdpnlmrkcj2005" + this.id, "1", 1);
			localStringBuffer.append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
			localStringBuffer.append("<!--\n");
			localStringBuffer.append("function cjselectTab");
			localStringBuffer.append(this.id);
			localStringBuffer.append("(n,pac,piac) {\n");
			localStringBuffer.append("var panelID='");
			localStringBuffer.append(this.id);
			localStringBuffer.append("';\n");
			if (this.panels == null)
				localStringBuffer.append("var numDiv=0;\n");
			else
				localStringBuffer.append("var numDiv=" + this.panels.size() + ";\n");
			localStringBuffer.append("var panelDiv;\n");
			localStringBuffer.append("var tabDiv;\n");
			if (this.panels != null) {
				for (i = 0; i < this.panels.size(); ++i) {
					String str;
					localPanel = (Panel) this.panels.elementAt(i);
					if ((str = localPanel.getScript()) == null)
						continue;
					++l;
					localStringBuffer.append("if (n==" + i + ") {\n");
					localStringBuffer.append(str);
					localStringBuffer.append(";}\n");
					localStringBuffer.append("else {\n");
				}
				while (l > 0) {
					localStringBuffer.append("}");
					--l;
				}
			}
			localStringBuffer.append("\nfor(var i=0; i<numDiv; i++) {\n");
			localStringBuffer.append("if (document.layers) {\n");
			localStringBuffer.append("panelDiv=document.layers[panelID+'panel'+i];\n");
			localStringBuffer.append("tabDiv=document.layers[panelID+'tab'+i]; }\n");
			localStringBuffer.append("else\n");
			localStringBuffer.append("if (document.all) {\n");
			localStringBuffer.append("panelDiv=document.all[panelID+'panel'+i];\n");
			localStringBuffer.append("tabDiv=document.all[panelID+'tab'+i]; }\n");
			localStringBuffer.append("else\n");
			localStringBuffer.append("if (document.getElementById) {\n");
			localStringBuffer.append("panelDiv=document.getElementById(panelID+'panel'+i);\n");
			localStringBuffer.append("tabDiv=document.getElementById(panelID+'tab'+i); }\n");
			localStringBuffer.append("z=panelDiv.style.zIndex;\n");
			localStringBuffer.append("if (i==n) z=numDiv;\n");
			localStringBuffer.append("else z=(numDiv-i);\n");
			localStringBuffer.append("panelDiv.style.zIndex=z;\n");
			localStringBuffer.append("tabDiv.style.zIndex = z;\n");
			localStringBuffer.append("if (pac[i]!='' && i==n) tabDiv.style.backgroundImage='url('+pac[i]+')';\n");
			localStringBuffer.append("if (piac[i]!='' && i!=n) tabDiv.style.backgroundImage='url('+piac[i]+')';\n");
			if ((this.activeColor != null) && (this.inactiveColor != null)) {
				localStringBuffer.append("if (i==n) tabDiv.style.backgroundColor='");
				localStringBuffer.append(this.activeColor);
				localStringBuffer.append("';\n");
				localStringBuffer.append("else      tabDiv.style.backgroundColor='");
				localStringBuffer.append(this.inactiveColor);
				localStringBuffer.append("';\n");
			}
			localStringBuffer.append("} }\n");
			localStringBuffer.append("//-->\n");
			localStringBuffer.append("</script>\n");
			localStringBuffer.append("<style type=\"text/css\">\n");
			localStringBuffer.append("<!--\n");
			localStringBuffer.append(".");
			localStringBuffer.append("cjdflttbcls");
			localStringBuffer.append("{\n");
			localStringBuffer.append("position:absolute; cursor:pointer; font-weight:bold; text-align:center; border:1px;\n");
			localStringBuffer.append("border-color:#000000; border-style:outset; background-color:#d3d3d3; border-bottom-style:none;\n");
			localStringBuffer.append("margin:0px; }\n");
			localStringBuffer.append(".");
			localStringBuffer.append("cjdfltpnlclss");
			localStringBuffer.append("{\n");
			localStringBuffer
					.append("position:absolute; border:1px; border-color:#000000; background-color:#d3d3d3; border-style:outset; left:0px;\n");
			localStringBuffer.append("top:28px; margin:0px; padding:6px; }\n");
			localStringBuffer.append("-->\n");
			localStringBuffer.append("</style>\n");
		}
		localStringBuffer.append("<div ");
		localStringBuffer.append("id=\"");
		localStringBuffer.append(this.id);
		localStringBuffer.append("\"");
		localStringBuffer.append(" style=\"background-color:transparent;position:relative;");
		localStringBuffer.append("width:");
		localStringBuffer.append(this.width);
		localStringBuffer.append(";");
		localStringBuffer.append("height:");
		localStringBuffer.append(this.height);
		localStringBuffer.append(";\">\n");
		if (this.panels != null) {
			PanelTab localPanelTab1;
			int j = -1;
			for (i = 0; i < this.panels.size(); ++i) {
				localPanel = (Panel) this.panels.elementAt(i);
				localPanel.setIndex(this.panels.size() - i);
				if (!(localPanel.getSelected()))
					continue;
				j = i;
			}
			if (j > 0) {
				localPanel = (Panel) this.panels.elementAt(j);
				j = localPanel.getIndex();
				localPanel.setIndex(((Panel) this.panels.elementAt(0)).getIndex());
				((Panel) this.panels.elementAt(0)).setIndex(j);
			}
			for (i = 0; i < this.panels.size(); ++i) {
				localPanel = (Panel) this.panels.elementAt(i);
				localPanelTab1 = localPanel.getPanelTab();
				if (localPanelTab1.getLeft() >= 0)
					continue;
				if (i == 0) {
					localPanelTab1.setLeft(0);
				} else {
					int k = 0;
					for (j = 0; j < i; ++j) {
						localPanel = (Panel) this.panels.elementAt(j);
						PanelTab localPanelTab2 = localPanel.getPanelTab();
						k += localPanelTab2.getLeft();
						k += getInteger(localPanelTab2.getWidth());
						++k;
					}
					localPanelTab1.setLeft(k);
				}
			}
			localStringBuffer.append("\n<script language=\"JavaScript\" type=\"text/javascript\">\n");
			localStringBuffer.append("var pac");
			localStringBuffer.append(this.id);
			localStringBuffer.append("=new Array(");
			for (i = 0; i < this.panels.size(); ++i) {
				localPanel = (Panel) this.panels.elementAt(i);
				localPanelTab1 = localPanel.getPanelTab();
				if (i != 0)
					localStringBuffer.append(",");
				if (localPanelTab1.getActiveBackgroundImage() != null) {
					localStringBuffer.append("'");
					localStringBuffer.append(localPanelTab1.getActiveBackgroundImage());
					localStringBuffer.append("'");
				} else {
					localStringBuffer.append("''");
				}
			}
			localStringBuffer.append(");\n");
			localStringBuffer.append("var piac");
			localStringBuffer.append(this.id);
			localStringBuffer.append("=new Array(");
			for (i = 0; i < this.panels.size(); ++i) {
				localPanel = (Panel) this.panels.elementAt(i);
				localPanelTab1 = localPanel.getPanelTab();
				if (i != 0)
					localStringBuffer.append(",");
				if (localPanelTab1.getInactiveBackgroundImage() != null) {
					localStringBuffer.append("'");
					localStringBuffer.append(localPanelTab1.getInactiveBackgroundImage());
					localStringBuffer.append("'");
				} else {
					localStringBuffer.append("''");
				}
			}
			localStringBuffer.append(");\n");
			localStringBuffer.append("</script>\n");
			for (i = 0; i < this.panels.size(); ++i) {
				localPanel = (Panel) this.panels.elementAt(i);
				PanelBody localPanelBody = localPanel.getPanelBody();
				localPanelTab1 = localPanel.getPanelTab();
				localStringBuffer.append("<div id=\"");
				localStringBuffer.append(this.id);
				localStringBuffer.append("panel" + i);
				localStringBuffer.append("\"");
				localStringBuffer.append(" class=\"");
				localStringBuffer.append(localPanel.getClassName());
				localStringBuffer.append("\"");
				localStringBuffer.append(" style=\"");
				localStringBuffer.append("width:");
				localStringBuffer.append(this.width);
				localStringBuffer.append(";");
				localStringBuffer.append("height:");
				localStringBuffer.append(this.height);
				localStringBuffer.append(";");
				localStringBuffer.append("z-index:" + localPanel.getIndex());
				localStringBuffer.append(";\">\n");
				localStringBuffer.append(localPanelBody.getHtmlCode());
				localStringBuffer.append("</div>\n");
				localStringBuffer.append("<div id=\"");
				localStringBuffer.append(this.id);
				localStringBuffer.append("tab" + i);
				localStringBuffer.append("\"");
				if (localPanel.getEnabled()) {
					localStringBuffer.append(" onClick=\"");
					localStringBuffer.append("cjselectTab");
					localStringBuffer.append(this.id);
					localStringBuffer.append("(" + i + ",pac");
					localStringBuffer.append(this.id);
					localStringBuffer.append(",piac");
					localStringBuffer.append(this.id);
					localStringBuffer.append(")\"");
				}
				if (localPanelTab1.getTitle() != null) {
					localStringBuffer.append(" title=\"");
					localStringBuffer.append(localPanelTab1.getTitle());
					localStringBuffer.append("\"");
				}
				localStringBuffer.append(" class=\"");
				localStringBuffer.append(localPanelTab1.getClassName());
				localStringBuffer.append("\"");
				localStringBuffer.append(" style=\"");
				localStringBuffer.append("width:");
				localStringBuffer.append(localPanelTab1.getWidth());
				localStringBuffer.append(";");
				localStringBuffer.append("height:");
				localStringBuffer.append(localPanelTab1.getHeight());
				localStringBuffer.append(";");
				localStringBuffer.append("left:" + localPanelTab1.getLeft());
				localStringBuffer.append("px;");
				localStringBuffer.append("z-index:" + localPanel.getIndex());
				localStringBuffer.append(";\">\n");
				localStringBuffer.append(localPanelTab1.getHtmlCode());
				localStringBuffer.append("</div>\n");
			}
		}
		localStringBuffer.append("</div>");
		if (this.lastWish != null) {
			localStringBuffer.append("\n");
			localStringBuffer.append(this.lastWish);
			localStringBuffer.append("\n");
		}
		localBodyContent.clearBody();
		try {
			localJspWriter.print(localStringBuffer.toString());
		} catch (Exception localException) {
			throw new JspException("tabpanel: Could not write body");
		}
		return 0;
	}

	public int doEndTag() throws JspException {
		dropData();
		return 6;
	}

	public void relese() {
		dropData();
	}

	private void dropData() {
		this.id = null;
		this.width = null;
		this.height = null;
		this.lastWish = null;
		this.activeColor = "#d3d3d3";
		this.inactiveColor = "#e8e8e8";
		this.panels = null;
	}

	private int getInteger(String paramString) {
		StringBuffer localStringBuffer = new StringBuffer("0");
		for (int i = 0; i < paramString.length(); ++i) {
			char c = paramString.charAt(i);
			if (!(Character.isDigit(c)))
				continue;
			localStringBuffer.append(c);
		}
		return Integer.parseInt(localStringBuffer.toString());
	}
}